/*
 * Copyright 2021-2022 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */
package org.jsets.fastboot.operlog;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 
 * OperLog上下文
 * 
 * @author wangjie (https://github.com/wj596)
 * @date 2021.07.10 23:56
 *
 */
public class OperLogContext {

	private final static ThreadLocal<Map<String, Object>> CTX = ThreadLocal.withInitial(()->new HashMap<String, Object>());

	public static void put(String key, Object val) {
		if (key == null) {
			throw new IllegalArgumentException("key cannot be null");
		}
		
		Map<String, Object> map = CTX.get();
		map.put(key, val);
	}

	public static Object get(String key) {
		return CTX.get().get(key);
	}

	public static void remove(String key) {
		CTX.get().remove(key);
	}

	public static void cleanup() {
		CTX.remove();
	}

	public static Set<String> getKeys() {
		return CTX.get().keySet();
	}

	public static boolean isEmpty() {
		return CTX.get().isEmpty();
	}

	public Map<String, Object> getCopyOfContextMap() {
		Map<String, Object> oldMap = CTX.get();
		return new HashMap<String, Object>(oldMap);
	}

}